package com.etoak.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Session;

/**
 * @program: activemq-SpringMVC
 * @description: Activemq
 * @author: 管浩杰
 * @create: 2021-03-17 11:34
 **/

@Configuration
@PropertySource(value = "classpath:mq.properties")
public class ActiveMQConfig {
    @Value("${mq.username}")
    private String username;

    @Value("${mq.password}")
    private String password;

    @Value("${mq.brokerURL}")
    private String url;


    /* ActiveMQConnectionFactory */

    @Bean
    public ActiveMQConnectionFactory mqConnectionFactory(){
        return new ActiveMQConnectionFactory(username, password, url);
    }

    /* CatchingConnectionFactory */
    @Bean
    public ConnectionFactory connectionFactory(){
        CachingConnectionFactory factory = new CachingConnectionFactory();
        factory.setTargetConnectionFactory(this.mqConnectionFactory());
        factory.setSessionCacheSize(30);
        return factory;
    }


    /* JMSTemplate  */

    @Bean
    public JmsTemplate jmsTemplate(){
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(this.connectionFactory());
        /* 开启服务质量检测 */
        jmsTemplate.setExplicitQosEnabled(true);
        /* 持久化 */
        jmsTemplate.setDeliveryMode(DeliveryMode.PERSISTENT);
        /* 客户端签收信息 */
        jmsTemplate.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
        return jmsTemplate;
    }
}
